package combat;



import java.util.ArrayList;

import exploration.Exploration;
import graphinterface.Barre;
import graphinterface.MouseDetector;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import savesystem.PlayerStat;


public class Combat extends BasicGameState{

	public static final int ID = 4;
	
	public Rectangle cadre;
	
	private Barre barreVie;													//la barre de vie
	private Barre barreMana;												//la barre de fer
	
	private MouseDetector boutonAttaque;									//bouton
	private MouseDetector boutonSoin;										//bouton
	private MouseDetector boutonPotion;										//bouton
	private Color[] couleurBouton = {Color.cyan, Color.white, Color.blue};
	
	private static PlayerStat ps;											//les stats du joueur
	
	public String phrase = "";
	public int x, y;
	
	public int tempsAttaque = 2000, timer = 0;
	
	public int vieMax = 100; 
	public int manaMax = 100; 
	public Ennemies ennemie = new Ennemies("Troll des montagnes", 1000, 10, 100, 200, 50);
	public String nomEnnemie = ennemie.getNomEnnemie();
	public int vieEnnemieMax = ennemie.getVieEnnemie(), vieEnnemie = vieEnnemieMax;
	
	public int arme = 10, armeEnnemie = 10;
	public int attaqueMin = 5, attaqueMax = 10;
	
	public Color colorAttaque = Color.cyan, colorSoin = Color.cyan;		
	
	public int tailleBarreDeVie = 110;
	
	public Music fond;
	
	public FenetreSecond niah;
	public ArrayList contenu;
	public String retour;
	
	public Arme armeAleat = new Arme();

	
	public void init(GameContainer container, StateBasedGame game) throws SlickException {
		
		barreVie = new Barre(container.getWidth()-120, container.getHeight()-95, 
				tailleBarreDeVie, 20, 
				vieMax, Color.red);
		barreVie.setContour(true);
		
		barreMana = new Barre(container.getWidth()-120, container.getHeight()-75, 
				tailleBarreDeVie, 20, 
				manaMax, Color.green);
		barreMana.setContour(true);
		
		boutonAttaque = new MouseDetector(new Rectangle(4,container.getHeight()-96,110,20), 
				couleurBouton, "Attaquer");
		
		boutonSoin = new MouseDetector(new Rectangle(4,container.getHeight()-74,110,20), 
				couleurBouton, "Soigner");
		boutonPotion = new MouseDetector(new Rectangle(4,container.getHeight()-52,110,20), 
				couleurBouton, "Potions");
		
		ps = new PlayerStat();
		
		cadre = new Rectangle (1,container.getHeight()-100,container.getWidth()-2,99);
		//fond = new Music ("res/testJeu.wav");
	    //fond.loop();
		
		armeAleat.armeAleat();
		String antique = "";
		String badass = "";
		String classeArme = armeAleat.getClasse();
		String nomArme = armeAleat.getNom();
		String typeArme = armeAleat.getType();
		int attaqueMin = armeAleat.getMin();
		int attaqueMax = armeAleat.getMax();
		int levelArme = armeAleat.getLevel();
		
		if (typeArme != "Normal") antique =  " antique";
		if (levelArme >= 50) badass = " badass";
	
		System.out.println(classeArme + " en " + nomArme + antique + badass);
		System.out.println("type: " + typeArme);
		System.out.println(attaqueMin + "-" + attaqueMax);
		System.out.println("lvl: " + levelArme);
		
		ennemie.EnnemieAleat();
		nomEnnemie = ennemie.getNomEnnemie();
		vieEnnemie = ennemie.getVieEnnemie();
		
		niah = new FenetreSecond ("Potions",50, 50, 100, 200, Color.white, Color.cyan);
		int posX = niah.getPosX();
		int posY = niah.getPosY();
		Rectangle Ritem1 = new Rectangle(posX + 2,posY + 20,50,50);
		Rectangle Ritem2 = new Rectangle(posX + 2,posY + 80,50,50);
		Color c[] = {Color.white, Color.cyan, Color.blue};
		MouseDetector petitePotion = new MouseDetector(Ritem1, c, "petite potion");
		MouseDetector potionMax = new MouseDetector(Ritem2, c, "potion max");
		contenu = new ArrayList();
		contenu.add(petitePotion);
		contenu.add(potionMax);
		niah.setContenu(contenu);
		
	}

	public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
		g.setColor(Color.white);
		g.draw(cadre);
		
		g.setColor(Color.cyan);
		g.drawString(phrase,5,50);
		
		g.setColor(Color.red);
		g.drawString("vie:" + ps.vie, container.getWidth()-200, container.getHeight()-95);
		g.drawString("Ennemie:" + nomEnnemie, container.getWidth()-400, 80);
		g.drawString("vie Ennemie:" + vieEnnemie, container.getWidth()-400, 100);
		
		barreVie.render(g);
		barreMana.render(g);
		boutonAttaque.draw(g);
		boutonSoin.draw(g);
		boutonPotion.draw(g);
		
		g.setColor(Color.green);
		g.drawString("mana:" + ps.mana,container.getWidth()-200,container.getHeight()-75);
		
		niah.draw(container, g, true);
	}
	
	
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
		
		if (niah.isShow()){
			niah.updateWHide(container);
			retour = niah.getPushed();
			System.out.println(retour);
		}
		if (retour != null){
			if (retour == "petite potion"){
			soinPotion(10, "Glouglouglou");
			}
			else if (retour == "potion max"){
				soinPotion(100, "Gros Glouglouglou");
				}
		}
		
		timer -= delta;
		if (timer <= 0){
			phrase = "";
			//niah.cacher();
		}

		this.updateBoutons(container);
		
		this.updateInput(container, game);
		
		barreVie.update(ps.vie);
		barreMana.update(ps.mana);
	}
	
	public void updateBoutons(GameContainer container){
		boutonAttaque.update(container, 0);
		boutonSoin.update(container, 0);
		boutonPotion.update(container, 0);
	}
	
	public void updateInput(GameContainer container, StateBasedGame game){
		x = container.getInput().getMouseX();
		y = container.getInput().getMouseY();
		
		if (container.getInput().isKeyPressed(Input.KEY_B)){
			ps.vie -= 10;
			if(ps.vie <= 0)
				ps.vie = 0;
		}

		if (boutonAttaque.isPushed() && timer <= 0){
			attaquer();
			
			
		}else if (boutonSoin.isPushed() && timer <= 0){
			soigner();
		}
		
		else if (boutonPotion.isPushed() && timer <= 0){
			niah.afficher();
			
		}
		
		boutonAttaque.resetPushed();
		boutonSoin.resetPushed();
		boutonPotion.resetPushed();
		
		if (container.getInput().isKeyDown(Input.KEY_C)){
			game.enterState(Exploration.ID);
		}
		niah.initPushed();
		retour = null;
	}
	
	public void attaquer(){
		if (vieEnnemie - arme >= 0){

			
			arme = armeAleat.attaqueRenvoi();
			vieEnnemie = vieEnnemie - arme;
			phrase = "attaquer!";
			timer = tempsAttaque;
		}else{
			vieEnnemie = 0;
		}
	}
	
	public void soigner(){
		if (ps.vie < vieMax){
			if (ps.vie + 10 < vieMax){
			ps.vie = ps.vie + 10;
			}
			else{
				ps.vie = vieMax;
			}
			phrase = "Soin";
			timer = tempsAttaque;
			if (ps.mana - 10 >= 0){
				ps.mana = ps.mana - 10;
				}
			else{
				ps.mana = 0;
			}
		}else{
			ps.vie = vieMax;
		}
	}
	public void soinPotion(int soin, String str){
			if (ps.vie < vieMax){
				if (ps.vie + soin < vieMax){
				ps.vie = ps.vie + soin;
				}
				else{
					ps.vie = vieMax;
				}
				phrase = str;
				timer = tempsAttaque;
			}else{
				ps.vie = vieMax;
			}
	}
	
	
	
	
	
	
	public void enter(GameContainer container, StateBasedGame game){
		ps = PlayerStat.Load();
	}
	public void leave(GameContainer container, StateBasedGame game){
		ps.Save();
	}	
	public int getID() {
		return ID;
	}
}